package com.edward.service;


import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.StatItem;


public class StatItemManager extends HibernateDaoSupport {

	public void addStatItem(StatItem statItem) {
		
		getHibernateTemplate().save(statItem);
	}
	
	public void updateStatItem(StatItem statItem) {
		
		getHibernateTemplate().update(statItem);
	}
	
	public void deleteStatItem(int id){
		
		getHibernateTemplate().delete(getStatItem(id));
	}
	
	public StatItem getStatItem(int id){
		StatItem d =(StatItem)getHibernateTemplate().get(StatItem.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<StatItem> listStatItem() {
		return getHibernateTemplate().find("from StatItem");
	}

	
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerStatItem(int start,int limit,int id) {
		List <StatItem>list = getHibernateTemplate().find("from StatItem i  where STAT_ID = " + id );
		StatItem s = new StatItem();
		s.setCode("合计");
		for (StatItem statItem : list) {
			s.setDmoney(s.getDmoney() + statItem.getDmoney());
			s.setRmoney(s.getRmoney() + statItem.getRmoney());
		}
		list.add(s);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		
		
		List <StatItem>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	


}



